<?php

namespace app\api\controller;

use app\admin\model\ad\Ad;
use app\admin\model\article\ArticleSee;
use app\admin\model\article\Message;
use app\admin\model\article\OnlineVideo;
use app\admin\model\article\Works;
use app\admin\model\course\Category;
use app\admin\model\user\Student;
use app\admin\model\user\TeacherCategory;
use app\admin\model\UserCollection;
use app\admin\model\article\WorksCategory;
use app\common\controller\Api;
use think\Db;
use think\Validate;

/**
 * 课程模块
 * @ApiWeigh (40)
 */
class Course extends Api
{
    protected $noNeedLogin = ["getBaseInfo","getCourseList","getCourseInfo","getSpellCourseList","getSpellCourseInfo"];
    protected $noNeedRight = ['*'];



    /**
     * 获取基础信息
     * @ApiWeigh (110)
     * @ApiMethod   (POST)
     * @ApiReturnParams   (name="category", type="object", required=true, description="课程分类")
     */
    public function getBaseInfo(){
        $categoryModel = new TeacherCategory();
        try {
            $categoryList = $categoryModel->getList();
            //轮播
            $adList = Ad::where("type", Ad::TYPE_PLATFORM_COURSE_HEADER)
                ->where("switch", 1)
                ->order("weigh", "desc")
                ->select();
            foreach ($adList as $key => $value){
                $value["image"] = deal_img_url($value["image"]);
            }
            //优惠券
            $couponModel = new \app\admin\model\course\Coupon();
            $condition = [];
            $couponList = $couponModel->getList($this->auth->getUser(),$condition,0,2);


        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $res = [
            "categoryList" => $categoryList,
            "adList" => $adList,
            "couponList" => $couponList,
        ];
        $this->success("查询成功", $res);
    }
    /**
     * 获取拼团列表
     * @ApiWeigh (100)
     * @ApiMethod   (POST)
     *
     * @ApiReturnParams   (name="spell_price.spell_price", type="string", required=true, description="拼团价格")
     * @ApiReturnParams   (name="course.name", type="string", required=true, description="课程名称")
     * @ApiReturnParams   (name="course.desc", type="string", required=true, description="简介")
     * @ApiReturnParams   (name="course.image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="course.price", type="string", required=true, description="售价")
     * @ApiReturnParams   (name="course.original_price", type="string", required=true, description="原价")
     * @ApiReturnParams   (name="course.class_hour", type="string", required=true, description="课时")
     */
    public function getSpellCourseList(){
        $spellModel = new \app\admin\model\course\Spell();
        $condition = [];

        try {
            $courseList = $spellModel->getList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }

        $this->success("查询成功", $courseList);
    }

    /**
     * 获取拼团详情
     * @ApiWeigh (99)
     * @ApiMethod   (POST)
     *
     * @ApiParams   (name=id, type="string", required=true, description="拼团id")
     * @ApiParams   (name="lng", type="string", required=true, description="经度")
     * @ApiParams   (name="lat", type="string", required=true, description="纬度")
     *
     * @ApiReturnParams   (name="spell_price.spell_price", type="string", required=true, description="拼团价格")
     * @ApiReturnParams   (name="course.name", type="string", required=true, description="课程名称")
     * @ApiReturnParams   (name="course.desc", type="string", required=true, description="简介")
     * @ApiReturnParams   (name="course.image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="course.price", type="string", required=true, description="售价")
     * @ApiReturnParams   (name="course.original_price", type="string", required=true, description="原价")
     * @ApiReturnParams   (name="course.buy_num", type="string", required=true, description="已抢人数")
     * @ApiReturnParams   (name="course.surplus_num", type="string", required=true, description="剩余人数")
     * @ApiReturnParams   (name="course.user_list.surplus_num", type="string", required=true, description="团组剩余人数")
     * @ApiReturnParams   (name="course.user_list.residue_time", type="string", required=true, description="团组剩余时间")
     */
    public function getSpellCourseInfo(){

        $spellModel = new \app\admin\model\course\Spell();

        $id = $this->request->post("id");
        $lng = $this->request->post("lng",'');
        $lat = $this->request->post("lat",'');
        $condition = [
            "id" => $id
        ];
        try {
            $info = $spellModel->getInfo($condition,$this->auth->getUser(),$lat,$lng);

        }catch (\Exception $e){
            $this->error($e->getMessage());
        }

        $this->success("查询成功", $info);
    }
    /**
     * 获取课程列表
     * @ApiWeigh (90)
     * @ApiMethod   (POST)
     * @ApiParams   (name="search", type="string", required=true, description="搜索")
     * @ApiParams   (name="category_id", type="string", required=true, description="课程分类id")
     *
     * @ApiReturnParams   (name="name", type="string", required=true, description="课程名称")
     * @ApiReturnParams   (name="desc", type="string", required=true, description="简介")
     * @ApiReturnParams   (name="image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="price", type="string", required=true, description="售价")
     * @ApiReturnParams   (name="original_price", type="string", required=true, description="原价")
     * @ApiReturnParams   (name="class_hour", type="string", required=true, description="课时")
     */
    public function getCourseList(){
        $courseModel = new \app\admin\model\course\Course();
        $category_id = $this->request->post("category_id");
        $search = $this->request->post("search");
        $condition = [];
        if($category_id){
            $condition["category_id"] = $category_id;
        }
        if($search){
            $condition["name|desc"] = ["LIKE","%".$search."%"];
        }
        try {
            $courseList = $courseModel->getList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }

        $this->success("查询成功", $courseList);
    }

    /**
     * 获取课程详情
     * @ApiWeigh (89)
     * @ApiMethod   (POST)
     * @ApiParams   (name="id", type="string", required=true, description="课程id")
     * @ApiParams   (name="lng", type="string", required=true, description="经度")
     * @ApiParams   (name="lat", type="string", required=true, description="纬度")
     *
     * @ApiReturnParams   (name="name", type="string", required=true, description="课程名称")
     * @ApiReturnParams   (name="desc", type="string", required=true, description="简介")
     * @ApiReturnParams   (name="image", type="string", required=true, description="图片")
     * @ApiReturnParams   (name="price", type="string", required=true, description="售价")
     * @ApiReturnParams   (name="original_price", type="string", required=true, description="原价")
     * @ApiReturnParams   (name="class_hour", type="string", required=true, description="课时")
     * @ApiReturnParams   (name="is_collection", type="string", required=true, description="是否收藏")
     */
    public function getCourseInfo(){
        $courseModel = new \app\admin\model\course\Course();
        $id = $this->request->post("id");
        $lng = $this->request->post("lng",'');
        $lat = $this->request->post("lat",'');
        $condition = [
            "id" => $id
        ];
        try {
            $info = $courseModel->getInfo($condition,$this->auth->getUser(),$lat,$lng);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }

        $this->success("查询成功", $info);
    }


    /**
     * 收藏课程
     * @ApiMethod   (POST)
     * @ApiWeigh (88)
     * @ApiParams   (name="id", type="int", required=true, description="课程id")
     *
     */
    public function collectionCourse(){
        $courseModel = new \app\admin\model\course\Course();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
            ];
            $info = $courseModel->getInfo($condition);
            $userCollectionModel->collectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_COURSE);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("收藏成功");
    }


    /**
     * 取消收藏课程
     * @ApiMethod   (POST)
     * @ApiWeigh (87)
     * @ApiParams   (name="id", type="int", required=true, description="课程id")
     *
     */
    public function cancelCollectionCourse(){
        $courseModel = new \app\admin\model\course\Course();
        $id = $this->request->post("id");
        try {
            $userCollectionModel = new UserCollection();
            $condition = [
                "id" => $id,
            ];
            $info = $courseModel->getInfo($condition,$this->auth->getUser());
            $userCollectionModel->cancelCollectionInfo($this->auth->getUser(),$info["id"],$userCollectionModel::TYPE_COURSE);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("取消成功");
    }

    /**
     * 收藏课程列表
     * @ApiMethod   (POST)
     * @ApiWeigh (86)
     *
     * @ApiParams   (name="page", type="int", required=true, description="页码")
     *
     */
    public function collectionCourseList(){
        try {
            $userCollectionModel = new UserCollection();
            $list = $userCollectionModel->collectionInfoList($this->auth->getUser(),$userCollectionModel::TYPE_COURSE);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }


    /**
     * 学员列表
     * @ApiMethod   (POST)
     * @ApiWeigh (80)
     *
     *
     */
    public function studentList(){
        try {
            $studentModel = new Student();
            $condition = [
                "user_id" => $this->auth->id
            ];
            $list = $studentModel->getList($condition);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$list);
    }

    /**
     * 学员详情
     * @ApiMethod   (POST)
     * @ApiWeigh (79)
     * @ApiParams   (name="id", type="string", required=true, description="学员id")
     *
     */
    public function studentInfo(){
        $id = $this->request->post("id");
        try {
            $studentModel = new Student();
            $condition = [
                "id" => $id,
                "user_id" => $this->auth->id,
            ];
            $student = $studentModel->where($condition)->find();
            if(!$student){
                exception("未找到该学员");
            }
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("查询成功",$student);
    }

    /**
     * 添加学员
     * @ApiMethod   (POST)
     * @ApiWeigh (78)
     * @ApiParams   (name="name", type="string", required=true, description="学员姓名")
     * @ApiParams   (name="gender", type="string", required=true, description="性别：0：女；1：男")
     * @ApiParams   (name="birthday", type="string", required=true, description="出生日期：2023-01-01")
     * @ApiParams   (name="school_name", type="string", required=true, description="学校")
     * @ApiParams   (name="id_card", type="string", required=true, description="身份证号")
     *
     */
    public function addStudent(){
        $data = $this->request->post();
        try {
            $studentModel = new Student();

            $data["user_id"] = $this->auth->id;
            $studentModel->addStudent($data);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("添加成功");
    }

    /**
     * 编辑学员
     * @ApiMethod   (POST)
     * @ApiWeigh (77)
     * @ApiParams   (name="id", type="string", required=true, description="学员id")
     * @ApiParams   (name="name", type="string", required=true, description="学员姓名")
     * @ApiParams   (name="gender", type="string", required=true, description="性别：0：女；1：男")
     * @ApiParams   (name="birthday", type="string", required=true, description="出生日期：2023-01-01")
     * @ApiParams   (name="school_name", type="string", required=true, description="学校")
     * @ApiParams   (name="id_card", type="string", required=true, description="身份证号")
     *
     */
    public function editStudent(){
        $data = $this->request->post();
        try {
            $studentModel = new Student();
            $data["user_id"] = $this->auth->id;
            $studentModel->editStudent($data);
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("编辑成功");
    }

    /**
     * 删除学员
     * @ApiMethod   (POST)
     * @ApiWeigh (76)
     * @ApiParams   (name="id", type="string", required=true, description="学员id")
     *
     */
    public function delStudent(){
        $id = $this->request->post("id");
        try {
            $studentModel = new Student();
            $data["id"] = $id;
            $data["user_id"] = $this->auth->id;
            $student = $studentModel->where("user_id",$data['user_id'])
                ->where("id",$data['id'])->find();
            if(!$student){
                exception("未找到当前学员");
            }
            $student->delStudent();
        }catch (\Exception $e){
            $this->error($e->getMessage());
        }
        $this->success("删除成功");
    }


}
